package com.ouc.yyi.springcloud.gateway.route;

import com.ouc.yyi.springcloud.gateway.security.SecurityService;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//@Configuration
@RequiredArgsConstructor
public class RouteConfig {
    private static final String CARD = "lb://card";

    private static final String USER = "lb://user";


    private static final String LOADER = "lb://loader";



//    private final UserFilter userFilter;
//
//    private final UserCustomerFilter userCustomerFilter;


    private final SecurityService securityService;


    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("card", r -> r.path("/card/**")
                        //.filters(f -> f.filter(userFilter))
                        .filters(f -> f.modifyResponseBody(String.class, String.class, ((exchange, s) -> securityService.modifyResponse(exchange, s)))
                                .modifyRequestBody(String.class, String.class, ((exchange, s) -> securityService.modifyRequest(exchange, s)))
                                //.filter(userCustomerFilter)
                        )
                        .uri(CARD)
                ).route("user", r -> r.path("/user/**")
                        .filters(f -> f.modifyResponseBody(String.class, String.class, ((exchange, s) -> securityService.modifyResponse(exchange, s)))
                                .modifyRequestBody(String.class, String.class, ((exchange, s) -> securityService.modifyRequest(exchange, s))))
                        .uri(USER)
                ).route("loader", r -> r.path("/loader/**")
                        .uri(LOADER))
                .build();

    }

}
